<template>
  <div>
    <div>
      <template>
        <el-table
          ref="multipleTable"
          :data="tableData"
          tooltip-effect="dark"
          style="width: 100%"
          @selection-change="handleSelectionChange">
          <el-table-column
            type="selection"
            width="55">
          </el-table-column>
          //列表内容
          <el-table-column
            prop="id"
            label="ID">
          </el-table-column>
          <el-table-column
            prop="productName"
            label="产品名称">
          </el-table-column>
          <el-table-column
            prop="serialNumber"
            label="产品编号">
          </el-table-column>
          <el-table-column
            :formatter="Kindformatter"
            prop="productKind"
            label="产品种类">
          </el-table-column>
          <el-table-column
            :formatter="Patternformatter"
            prop="businessPattern"
            label="产品商业模式">
          </el-table-column>
          <el-table-column
            :formatter="Rulesformatter"
            prop="lendingRules"
            label="产品放款规则">
          </el-table-column>
          <el-table-column
            :formatter="Prepayformatter"
            prop="whetherPrepay"
            label="是否允许提前还款">
          </el-table-column>
          <el-table-column
            :formatter="Paymentformatter"
            prop="whetherPrepayPayment"
            label="是否允许部分还款">
          </el-table-column>
          <el-table-column
            prop="interestRate"
            label="利率">
          </el-table-column>
          <el-table-column
            :formatter="Statusformatter"
            prop="auditStatus"
            label="审核状态">
          </el-table-column>
          <el-table-column
            fixed="right"
            label="操作"
            width="100">
          </el-table-column>
        </el-table>
        <div style="margin-top: 20px">
          <el-button @click="toggleSelection([tableData[1], tableData[2]])">切换第二、第三行的选中状态</el-button>
          <el-button @click="toggleSelection()">取消选择</el-button>
        </div>
      </template>
    </div>
  </div>
</template>

<script>
import { getProductList } from '@/api/meapi-lyc'

export default {
  name: 'List',
  data() {
    return {
      tableData: [],
      multipleSelection: [],
      size: 1,
      current: 5,
      total: 0
    }
  },

  methods: {
    // 产品种类 formatter
    Kindformatter(row) {
      if (row.productKind === 1) {
        return '信用贷'
      } else if (row.productKind === 2) {
        return '抵押贷'
      } else {
        return '未知'
      }
    },
    // 产品商业模式 formatter
    Patternformatter(row) {
      if (row.businessPattern === 1) {
        return '自营贷'
      } else if (row.businessPattern === 2) {
        return '联合贷'
      } else {
        return '未知'
      }
    },
    // 产品放款规则 formatter
    Rulesformatter(row) {
      if (row.lendingRules === 1) {
        return '及时放款'
      } else if (row.lendingRules === 2) {
        return '延迟放款'
      } else {
        return '未知'
      }
    },
    // 是否允许提前还款 formatter
    Prepayformatter(row) {
      if (row.whetherPrepay === 0) {
        return '不允许'
      } else if (row.whetherPrepay === 1) {
        return '允许'
      } else {
        return '未知'
      }
    },
    // 是否允许部分还款 formatter
    Paymentformatter(row) {
      if (row.whetherPrepayPayment === 0) {
        return '不允许'
      } else if (row.whetherPrepayPayment === 1) {
        return '允许'
      } else {
        return '未知'
      }
    },
    // 审核状态 formatter
    Statusformatter(row) {
      if (row.auditStatus === 0) {
        return '待审核'
      } else if (row.auditStatus === 1) {
        return '审核通过'
      } else if (row.auditStatus === 2) {
        return '审核未通过'
      } else {
        return '未知'
      }
    },

    toggleSelection(rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    }
  },

  mounted() {
    const param = {
      'current': 1,
      'size': 5
    }
    getProductList(param).then(res => {
      console.log(res.data.records)
      this.tableData = res.data.records
      this.size = res.data.size
      this.total = res.data.total
      this.current = res.data.current
      console.log(this.tableData)
    })
  }
}
</script>

<style scoped>

</style>
